﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.Services;
using BLL;
using BLL.RF.Package;
using BLL.RF.ProductCategory;
using Model;
using Model.RF.Package;
using Model.RF.ProductCategory;

/// <summary>
///ProductPackage_PackintoBox_Service 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class ProductPackage_PackintoBox_Service : System.Web.Services.WebService
{
    private ClientSoft cs;
    private ProductType p;
    private ProductType_Pack ptp;
    private PackageProduct pp;
    private PackintoBox pb;
    private PackintoBox_WithoutUpload pbwu;
    private PackintoBoxItem pbi;
    private Package package;
    private SNUnion union;
    
    public ProductPackage_PackintoBox_Service()
    {
        cs = new ClientSoft();
        p = new ProductType();
        ptp = new ProductType_Pack();
        pp = new PackageProduct();
        pb = new PackintoBox();
        pbwu = new PackintoBox_WithoutUpload();
        pbi = new PackintoBoxItem();
        package = new Package();
        union = new SNUnion();
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod(Description = "判断装箱工序防错客户端软件的版本是否有效，有更新时提供版本号即可")]
    public bool IsValid_ClientSoft(string csVersion)
    {
        return !string.IsNullOrEmpty(cs.GetClientSoftbyNV("装箱工序防错客户端", csVersion).CSName) && cs.GetClientSoftbyNV("装箱工序防错客户端", csVersion).IsValid;
    }

    [WebMethod(Description = "是否有包装记录")]
    public bool ProductType_Flow_HasPackageRecord(string packageProductSN)
    {
        return (!string.IsNullOrEmpty(pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductSN) 
            && pp.GetPackageProductbyPackageProductSN(packageProductSN).IsPackage.Equals("Yes",StringComparison.OrdinalIgnoreCase))
            || !string.IsNullOrEmpty(package.LoadEntity(packageProductSN).PackageProductSN);
    }

    [WebMethod(Description = "通过SN，获取产品类型S代码，此方法在ProductType_Flow_HasPackageRecord之后调用")]
    public string GetProductTypeIDbyPackageProductSN(string packageProductSN)
    {
        string pid=pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductTypeID;
        if (string.IsNullOrEmpty(pid))
        {
            pid = union.SNUnion_GetbyPackageProductSN(packageProductSN)[0].ProductTypeID; 
        }
        return pid;
    }

    [WebMethod(Description = "通过产品类型S代码，获取相应的产品基础信息，包括产品类型名称:productTypeName、3BK条码:sN3BK等")]
    public ProductTypeInfo ProductType_GetbyProductTypeID(string productTypeID)
    {
        return p.LoadEntity(productTypeID);
    }

    [WebMethod(Description = "通过产品类型S代码，获取相应的装箱模板信息")]
    public ProductType_PackInfo ProductType_Pack_GetbyProductTypeID(string productTypeID)
    {
        return ptp.ProductType_Pack_GetbyPID(productTypeID);
    }

    [WebMethod(Description = "判断产品是否属于相应的产品类型，此方法也在ProductType_Flow_HasPackageRecord之后调用")]
    public bool IsValidProductTypeIDforPackageProductSN(string packageProductSN,string productTypeID)
    {
        switch (p.LoadEntity(productTypeID).HouseAmount)
        {
            case 2:
                return productTypeID.Equals(union.SNUnion_GetbyPackageProductSN(packageProductSN)[0].ProductTypeID, StringComparison.CurrentCultureIgnoreCase);
            default:
                return productTypeID.Equals(pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductTypeID, StringComparison.CurrentCultureIgnoreCase);
        }
    }

    [WebMethod(Description = "是否有装箱记录，装箱时返回false才允许操作，分箱时返回true才允许操作")]
    public bool ProductType_Flow_HasPackintoBoxRecord(string packageProductSN)
    {
        IList<PackintoBoxItemInfo> list=pbi.GetPackintoBoxItem_bySN(packageProductSN);
        foreach (PackintoBoxItemInfo pi in list)
        {
            if (!pi.IsUnpack)
                return true;
        }
        return false;
    }

    [WebMethod(Description = "批量装箱")]
    public bool PackintoBox_Insert(PackintoBoxInfo pbinfo, List<string> packageProductSNs)
    {
        DataTable dt = new DataTable(); 
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (string item in packageProductSNs)
        {
            dr = dt.NewRow();
            dr["ProductSN"] = item;
            dt.Rows.Add(dr);
        }
        return pb.PackintoBox_Insert(pbinfo,dt);
    }

    #region 20180224：增加可打印，不上传数据的功能
    [WebMethod(Description = "打印后，装箱数据不上传及其原因的上传")]
    public bool PackintoBox_WithoutUpload_Insert(PackintoBox_WithoutUploadInfo pbinfo, List<string> packageProductSNs)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ProductSN");
        DataRow dr;
        foreach (string item in packageProductSNs)
        {
            dr = dt.NewRow();
            dr["ProductSN"] = item;
            dt.Rows.Add(dr);
        }
        return pbwu.PackintoBox_WithoutUpload_Insert(pbinfo, dt);
    }
    #endregion

    #region 20180411：增加分箱工序
    [WebMethod(Description = "分箱操作记录，只需输入一个成品序列号，相应整箱产品全部从此箱中拆分掉")]
    public bool UnpackfromBox_Insert(string packageProductSN,string userID,string pCIP)
    {
        UnpackfromBoxInfo ui = new UnpackfromBoxInfo() { UserID = userID, PCIP = pCIP };
        UnpackfromBox ub = new UnpackfromBox();
        return ub.UnpackfromBox_Insert(ui, packageProductSN);
    }
    #endregion
}

